<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="550px"
    @close="handleClose">
    <el-form ref="modelForm" :model="modelForm" :rules="rules" label-width="80px">
      <el-form-item label="原密码" prop="oldPwd">
        <el-input v-model="modelForm.oldPwd" type="password" placeholder="原密码"/>
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input v-model="modelForm.newPwd" type="password" placeholder="新密码"/>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPwd">
        <el-input v-model="modelForm.checkPwd" type="password" placeholder="确认新密码"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm">取 消</el-button>
      <el-button :loading="isSubmit" type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import resultCode from '@/utils/resultCode'
import { updatePwdByAccountId } from '@/api/account'

export default {
  data() {
    var validateCheckPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.modelForm.newPwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      isSubmit: false,
      title: '修改密码',
      modelForm: {
        accountId: null,
        oldPwd: null,
        newPwd: null,
        checkPwd: null
      },
      rules: {
        oldPwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        checkPwd: [
          { required: true, message: '请确认新密码', trigger: 'blur' },
          { validator: validateCheckPass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(accountId) {
      this.modelForm.accountId = accountId
      this.modelForm.oldPwd = ''
      this.modelForm.newPwd = ''
      this.modelForm.checkPwd = ''
      this.visible = true
    },
    handleSubmit: function() {
      this.$refs.modelForm.validate((valid) => {
        if (!valid) {
          return false
        }
        this.isSubmit = true
        updatePwdByAccountId(this.modelForm).then(response => {
          const result = response.data // 请求接口返回的内容
          if (result.code === resultCode.SUCCESS) {
            if (result.data === 1) {
              this.visible = false
              this.$message.success('修改成功')
            } else {
              this.$message.error('修改失败，原密码不对')
            }
          }
          this.isSubmit = false
        }).catch(err => {
          this.isSubmit = false
        })
      })
    },
    resetForm: function() {
      this.$refs.modelForm.resetFields()
      this.visible = false
    },
    handleClose: function() {
      this.$refs.modelForm.resetFields()
      this.visible = false
      this.isSubmit = false
    }
  }
}
</script>
